package com.study.demo.ui.gallery;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.support.annotation.NonNull;

import com.stduy.injector.PerActivity;
import com.study.demo.bean.Folder;
import com.study.demo.bean.Image;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by sll on 2016/3/9.
 */
@PerActivity
public class GalleryPresenter implements GalleryContract.Presenter {

    private final String[] IMAGE_PROJECTION = {
            MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME,
            MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media._ID
    };

    private GalleryContract.View mGalleryView;
    private Disposable disposable;
    private List<Folder> folders = new ArrayList<>();
    private boolean hasFolderGened = false;

    private Context mContext;

    @Inject
    public GalleryPresenter(Context context) {
        mContext = context;
    }

    @Override
    public void attachView(@NonNull GalleryContract.View view) {
        mGalleryView = view;
    }

    @Override
    public void detachView() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
        mGalleryView = null;
    }

    @Override
    public void onImageAndFolderReceive() {
        disposable = Observable.create(this::observableOnSubscribe)
                .subscribeOn(Schedulers.io())
                .map(this::convertCursorToImages)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::onGetImages);
    }

    private void observableOnSubscribe(ObservableEmitter<Cursor> e) {
        Cursor cursor = MediaStore.Images.Media.query(mContext.getContentResolver(),
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_PROJECTION, null, null,
                IMAGE_PROJECTION[2] + " DESC");
        e.onNext(cursor);
    }

    private List<Image> convertCursorToImages(Cursor cursor) {
        List<Image> images = new ArrayList<Image>();
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            do {
                String path = cursor.getString(cursor.getColumnIndexOrThrow(IMAGE_PROJECTION[0]));
                String name = cursor.getString(cursor.getColumnIndexOrThrow(IMAGE_PROJECTION[1]));
                long dateTime = cursor.getLong(cursor.getColumnIndexOrThrow(IMAGE_PROJECTION[2]));
                Image image = new Image(path, name, dateTime);
                images.add(image);
                if (!hasFolderGened) {
                    // 获取文件夹名称
                    File imageFile = new File(path);
                    File folderFile = imageFile.getParentFile();
                    Folder folder = new Folder();
                    folder.name = folderFile.getName();
                    folder.path = folderFile.getAbsolutePath();
                    folder.cover = image;
                    if (!folders.contains(folder)) {
                        List<Image> imageList = new ArrayList<Image>();
                        imageList.add(image);
                        folder.images = imageList;
                        folders.add(folder);
                    } else {
                        // 更新
                        Folder f = folders.get(folders.indexOf(folder));
                        f.images.add(image);
                    }
                    hasFolderGened = true;
                }
            } while (cursor.moveToNext());
        }
        return images;
    }

    private void onGetImages(List<Image> images) {
        mGalleryView.renderImages(images);
        mGalleryView.renderFolders(folders);
    }
}
